<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.js"></script>
</head>

<body>
    <div class="app">
        <ul>
            <!-- 不同于index,p.id在虚拟dom作为唯一标识 不写key默认会用index作为key-->
            <li v-for="p in person" :key="p.id">{{p.name}}-{{p.age}} <input type="text"> </li>
        </ul>
        <button @click="add">添加老刘</button>
        <!-- key原理 
            1.初始数据->初始虚拟dom->真实dom
            2.更新数据->更新虚拟dom（虚拟dom对比 已生成的复用）->真实dom
            所以尽量不用index作为key
        -->
        <ul>
            <li v-for="(p,index) in person" :key="index">{{p.name}}-{{p.age}} <input type="text"> </li>
        </ul>
        <button @click="add">添加老刘</button>
    </div>
    <script>
        const vm = new Vue({
            data: {
                person: [{
                    id: '001',
                    name: '张三',
                    age: 18
                }, {
                    id: '002',
                    name: '李四',
                    age: 19
                }, {
                    id: '003',
                    name: '牛五',
                    age: 20
                }]
            },
            methods: {
                add() {
                    const p = {
                        id: '004',
                        name: '老刘',
                        age: 21
                    }
                    this.person.unshift(p)
                }
            }
        })
        vm.$mount('.app')
    </script>
</body>

</html>